package com.lingyistudio.user.controller;

import com.lingyistudio.common.response.WebResponse;
import com.lingyistudio.user.entity.UserEntity;
import com.lingyistudio.user.service.UserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @ClassName: userController
 * @Description:
 * @Author: cheng
 * @Date: 2019/5/24
 */
@CrossOrigin
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    UserService userService;

    @PostMapping("login")  //登陆
    public WebResponse UserLogin(UserEntity userEntity) {
        return userService.userLoginService(userEntity);
    }

    @PostMapping("registered")  //注册
    public WebResponse UserRegistered(UserEntity userEntity) {
        return userService.userRegisteredService(userEntity);
    }

    @PostMapping("updatePassWord")  //修改密码
    public WebResponse userUpdatePassWord(UserEntity userEntity) {
        return userService.userUpdatePassWordService(userEntity);
    }

    @PostMapping("updateInformation")  //修改其他
    public WebResponse userUpdateAll(UserEntity userEntity) {
        return userService.userUpdateAllService(userEntity);
    }


    @RequestMapping(value = "/upload")
    public String upload(MultipartFile file, HttpServletRequest request) throws IOException, InterruptedException {

        System.out.println(file.getName());
        String path = "F:/临时无用文件夹";
        String fileName = file.getOriginalFilename();//获取到上传文件的名字
        System.out.println("file.name: "+file.getName());//获取到file
        File dir = new File(path, fileName);
        System.out.println("WriteFile: "+file.getOriginalFilename()+"  "+file.getSize()+" B  "+ ( file.getSize()/1024 ) +" KB  "+( ( file.getSize()/1024/1024/1024 )>0 ?  ( file.getSize()/1024/1024/1024 )+" GB  ": ( file.getSize()/1024/1024) > 0 ?( file.getSize()/1024/1024)+" MB":" " ) );
        System.out.println("new file.getParentFile: "+dir.getParentFile());
        if (!dir.exists()) {
//            dir.mkdirs();//创建多级文件夹。
            System.out.println("创建文件夹");
            dir.getParentFile().mkdirs();
//            dir.mkdir();//创建文件夹
        }
    //MultipartFile自带的解析方法  
        System.out.println("file.getInputStream:  "+file.getInputStream());
        System.out.println("new file.path: "+dir.getPath());
        System.out.println("new file.name: "+dir.getName());
        Thread.sleep(10);
        file.transferTo(dir);

        return "/upload" + "/" + fileName;
    }

}
